# -*- coding: utf-8 -*-
import scrapy
from bs4 import BeautifulSoup
from fix_room.items import FixRoomItem
import pymysql

# 连接database
conn = pymysql.connect(host='127.0.0.1', user='root', password='newpassword', database='fix_room_db')
# 得到一个可以执行SQL语句的光标对象
cursor = conn.cursor()


class RoomInfoSpider(scrapy.Spider):
    sql = 'select href from t_room_href'
    cursor.execute(sql)
    urls = cursor.fetchall()

    i = 0


    name = 'room_info'
    allowed_domains = ['www.jtljia.com']
    # start_urls = ['https://www.jtljia.com/case/']
    # start_urls = ['https://www.jtljia.com/case/']

    def start_requests(self):
        # urls = []
        # # https://www.jtljia.com/case-p3/
        # for i in range(1,21):
        #     url = 'https://www.jtljia.com/case-p'+str(i)+'/'
        #     urls.append(url)
        for url in self.urls:
            self.i += 1
            yield scrapy.Request(url=url[0], callback=self.parse,meta={'i':self.i})


    def parse(self, response):
        i = response.meta['i']
        item = FixRoomItem()
        #爬取案例详情页 t_room_detail
        html = response.text
        soup = BeautifulSoup(html, 'lxml')
        s_part2 = soup.find(class_="caseIntroduction2").find_all('div')
        item = FixRoomItem()
        for s in s_part2:
            part2_title = s.h3.text
            part2_content = s.p.text
            part2_img = s.find_all('img')[1]['src']
            item['part2_title'] = part2_title
            item['part2_content'] = part2_content
            item['part2_img'] = part2_img
            item['i'] = i
            yield item


        # s_part1 = soup.find(class_="caseIntroduction1")
        # title = s_part1.h1.text
        # s_part1_style = s_part1.find_all('li')
        # room_style = s_part1_style[0].a.text
        # room_area = s_part1_style[1].a.text
        # room_count = s_part1_style[2].a.text
        # room_address = s_part1_style[3].find_all('span')[1].text
        # item['title'] = title
        # item['room_style'] = room_style
        # item['room_area'] = room_area
        # item['room_count'] = room_count
        # item['room_address'] = room_address
        # yield item





        pass
        # 爬取小分类 t_room_style
        # html = response.text
        # soup = BeautifulSoup(html, 'lxml')
        # soup = soup.find(id='case_filter').find_all('dl')  # [0].find_all('dd')
        # item = FixRoomItem()
        # for i in range(len(soup)):
        #
        #     sou = soup[i].find_all('dd')
        #     for s in  sou:
        #         item['i'] = i
        #         style = s.text
        #         item["style"] = style
        #         yield item

        # 爬取详情页地址
        # html = response.text
        # soup = BeautifulSoup(html, 'lxml')
        # s = soup.find(class_="case_list").find_all('li')
        # href_list = []
        # item = FixRoomItem()
        # for s1 in s:
        #     href = s1.a["href"]
        #     item['href'] = href
        #     yield item

